<template>
  <div class="app-container">
    <list-admin
      v-if="user>0"
      v-model="list"
      :entity-conf="entity"
      :list-display="fields"
      :list-filter="filters"
      :disabled-actions="disabled"
      :query="query"
      :config="config"
      :actions="actions"
    >
      <template v-slot:titleText>
        <span>工资发放</span>
      </template>
    </list-admin>
  </div>
</template>

<script>
import ListAdmin from '@/components/EasyAdmin/ListAdmin'
import admin from '@/config'

export default {
  components: { ListAdmin },
  data() {
    return {
      entity: '',
      alias: '',
      list: [],
      config: {},
      fields: [],
      filters: null,
      params: this.$route.query,
      user: 0
    }
  },
  async created() {
    // Load entities data
    this.alias = 'OfficerSalary'

    if (!Object.keys(admin.entities).includes(this.alias)) {
      console.log('NO!')
    } else {
      const user = await this.axios.get(`/api/user`, { params: {
        '@expands': `['entity.officer.store']`
      }})
      this.user = user.data.officer?.__metadata.store?.__metadata.user?.id
      if (!this.user) {
        this.$message.error('获取主账号信息失败！')
        return
      }
      this.config = admin.entities[this.alias]
      this.entity = Object.keys(this.config).includes('entity') ? this.config.entity : this.alias
      this.disabled = this.config.list.disabled_actions
      this.actions = this.config.list.actions
      this.query = {
        '@filter': `entity.getType() matches "salary" && entity.getFrom().getId() == ${this.user}`,
        '@expands': `['entity.to.staffs']`,
        '@order': 'entity.id|DESC'
      }
      console.log('params', this.params)
      this.filters = {
        type: Object.assign({ __label: '类型' }, {
          'salary': '手动发放',
          'salary-settle': '日薪结算',
          'salary-settle-leader': '日薪结算至队长',
          'salary-settle-borker': '日薪结算至经纪人'
        }),
        beforeCreatedTime: {
          expression: 'entity.getCreatedTime() >= datetime.get(":value  00:00:00")',
          label: '开始时间',
          type: 'date'
        },
        afterCreatedTime: {
          expression: 'entity.getCreatedTime() < datetime.get(":value 23:59:59")',
          label: '结束时间',
          type: 'date'
        }
      }
      if (this.params.date) {
        // date范围 [0]startTime [1]endTime
        if (this.filters.beforeCreatedTime) {
          this.filters.beforeCreatedTime.default = this.params.date[0]
        }
        if (this.filters.afterCreatedTime) {
          this.filters.afterCreatedTime.default = this.params.date[1]
        }
      }

      this.fields = this.config.list.list_display
    }
  }
}
</script>
